import AppConst from "../AppConst";
import LayerMgr from "../view/LayerMgr";
import AlertView from "./AlertView";


export default class Alert {
    private static _inst: Alert = null;
    public static get inst(): Alert { return this._inst || (this._inst = new Alert()); }

    private _alertPrefab: cc.Prefab = null;


    private constructor() {
        cc.loader.loadRes('mi/core/Alert', (err: Error, alertPrefab: cc.Prefab) => {
            if (!err) {
                this._alertPrefab = alertPrefab;
            } else {
                cc.error('[mi]找不到Res[mi/core/Alert], err:', err);
            }
        });
    }


    public show(content: string, cbOk?: Function, cbCancel?: Function): void {
        const panelAlert = cc.instantiate(this._alertPrefab);
        LayerMgr.setLayer(panelAlert, AppConst.viewLayer.Alert);

        const alertView = panelAlert.addComponent(AlertView);
        alertView.show(content, cbOk, cbCancel);
    }
}
